-- GENERAL
function ccR_Mage_Counterspell(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Mage_" .. specNumber .. "_UseCounterspell"]
	if vUseAbility
	and ccrPlayerHasSpell(COUNTERSPELL)
	and ccrSpellCanAttack(COUNTERSPELL)
	and ccrTargetIsCastingOrChanneling()
	and ccrTargetIsInteruptable()
	then
		ccrCastAttack(COUNTERSPELL)
		return
	end
	return
end
vccR_Last_Frost_Nova_Check = 0.1
function ccR_Mage_Frost_Nova(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Mage_" .. specNumber .. "_UseFrostNova"]
	if vUseAbility
	and ccrPlayerHasSpell(FROST_NOVA)
	and ccrSpellIsReady(FROST_NOVA)
	and #tblPGetMeleeEnemies > 0
	and ccrPlayerIsInCombat()
	and not ccrPlayerHasBuff(ICE_WARD)
	and (GetTime()-vccR_Last_Frost_Nova_Check) > 0.50
	then
		if # tblPGetMeleeEnemies >= 1 then
			for x = 1, # tblPGetMeleeEnemies do
				local obj = tblPGetMeleeEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				and not ccrUnitHasMyDebuff(obj, FROST_NOVA)
				and not ccrUnitHasMyDebuff(obj, ICE_WARD)
				then
					if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
					ccrCast(FROST_NOVA)
					return
				end
			end
		end
		vccR_Last_Frost_Nova_Check = GetTime()
	end
	return
end
function ccR_Mage_Mirror_Image(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Mage_" .. specNumber .. "_UseMirrorImage"]
	if vUseAbility
	and	ccrPlayerHasSpell(MIRROR_IMAGE)
	and ccrSpellIsReady(MIRROR_IMAGE)
	and ccrTargetIsBoss()
	then
		ccrCast(MIRROR_IMAGE)
	end
end
function ccR_Mage_Arcane_Explosion()
	if ccrPlayerHasSpell(ARCANE_EXPLOSION)
	and ccrSpellIsReady(ARCANE_EXPLOSION)
	then
		ccrCast(ARCANE_EXPLOSION)
		return
	end
end
function ccR_Mage_Deep_Freeze()
	if ccrPlayerHasSpell(DEEP_FREEZE)
	and ccrSpellCanAttack(DEEP_FREEZE)
	and not ccrTargetIsBoss()
	then
		if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end;
		ccrCastAttack(DEEP_FREEZE)
		return
	end
end
-- ARCANE
function ccR_Mage_Arcane_Moving()
	if ccrPlayerIsMoving()
	and not ccrPlayerHasBuff(ICE_FLOES)
	then
		ccR_Mage_Nether_Tempest()
		ccR_Mage_Living_Bomb()
		-- AoE While Moving
		if #tblPGetMeleeEnemies >= 3 then
			ccR_Mage_Arcane_Explosion()
		end
		-- Any Instant casts we can do
		if ccrValidTarget()
		and Player:IsFacing(Target)
		then
			ccR_Mage_Arcane_Barrage()
			ccR_Mage_Cone_of_Cold()
			ccR_Mage_FireBlast()
			ccR_Mage_Ice_Lance()
		end
	end
end
function ccR_Mage_Arcane_Missiles()
	if ccrPlayerHasSpell(ARCANE_MISSILES)
	and ccrSpellCanAttack(ARCANE_MISSILES)
	and Target and Target:Exists() and Player:IsFacing(Target)
	then
		ccrCastAttack(ARCANE_MISSILES)
		return
	end
	return
end
function ccR_Mage_Arcane_Barrage()
	if ccrPlayerHasSpell(ARCANE_BARRAGE)
	and ccrSpellCanAttack(ARCANE_BARRAGE)
	and Target and Target:Exists() and Player:IsFacing(Target)
	then
		ccrCastAttack(ARCANE_BARRAGE)
		return
	end
	return
end
function ccR_Mage_Arcane_Blast()
	if ccrPlayerHasSpell(ARCANE_BLAST)
	and ccrSpellCanAttack(ARCANE_BLAST)
	and Target and Target:Exists() and Player:IsFacing(Target)
	then
		ccrCastAttack(ARCANE_BLAST)
		return
	end
	return
end
function ccR_Mage_Arcane_Power()
	if ccrPlayerHasSpell(ARCANE_POWER)
	and ccrSpellIsReady(ARCANE_POWER)
	and ccrPlayerIsInCombat()
	and ccrValidTarget()
	then
		ccrCast(ARCANE_POWER)
		return
	end
	return
end
-- FIRE
function ccR_Mage_FireBlast()
	if ccrPlayerHasSpell(FIREBLAST)
	and ccrSpellCanAttack(FIREBLAST)
	and ccrValidTarget()
	and Player:IsFacing(Target)
	then
		ccrCastAttack(FIREBLAST)
		return
	end
	return
end
--[[
		FROST
]]
function ccR_Mage_Frostbolt()
	if ccrPlayerHasSpell(FROSTBOLT)
	and ccrSpellCanAttack(FROSTBOLT)
	and (not ccrPlayerIsMoving() or ccrPlayerHasBuff(ICE_FLOES))
	and ccrValidTarget()
	and Player:IsFacing(Target)
	then
		ccrCastAttack(FROSTBOLT)
		return
	end
	return
end

vLastFreezeGroupsCheck = 0.1
function ccR_Mage_Freeze_Groups()
	local vUseAbility = ccRS["Mage_3_UseFreezeGroups"]
	local vGroupSize = ccRS["Mage_3_FreezeGroupCount"]
	if vUseAbility
	and ccrPlayerHasSpell(FREEZE)
	and ccrSpellIsReady(FREEZE)
	and (GetTime()-vLastFreezeGroupsCheck) > 0.50
	and Target and Target:Exists() and ccrPetDistanceToTarget(Target) <= 45
	then
		local vTargetCount = ccrTargetCount(10)
		if vTargetCount >= vGroupSize then
			if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
			ccrCast(FREEZE)
			if SpellIsTargeting() == 1
			and Target
			and Target:Exists()
			and Pet and Pet:Exists() and Pet:GetDistance(Target) <= 45
			then
				CastAtLocation(Target:GetLocation())
			end
		end
		vLastFreezeGroupsCheck = GetTime()
		return
	end
	return
end

vLastIceLanceFrozenCheck = 0.1
function ccR_Mage_Ice_Lance_Frozen()
	local vUseAbility = ccRS["Mage_3_UseIceLanceAnyone"]
	if vUseAbility
	and ccrPlayerHasSpell(ICE_LANCE)
	and ccrSpellIsReady(ICE_LANCE)
	and (GetTime()-vLastIceLanceFrozenCheck) > 0.10
	then
		local x
		if # tblPGetNearbyEnemies >= 1 then
			for x = 1, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:IsTappedByPlayer() or (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName()))
				and obj:InLineOfSight()
				and Player:IsFacing(obj)
				and (	
						ccrUnitHasMyDebuff(obj, FROST_NOVA)
						or
						ccrUnitHasMyDebuff(obj, ICE_WARD)
						or
						ccrUnitHasMyDebuff(obj, DEEP_FREEZE)
						or
						ccrUnitHasMyDebuff(obj, FREEZE)
					)
				then
					if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end;
					--ccrCastAttack(ICE_LANCE, obj)
					local vIceLance = GetSpellInfo(ICE_LANCE)
					obj:CastSpellByName(vIceLance)
					return
				end
			end
		end
		vLastIceLanceFrozenCheck = GetTime()
		return
	end
end

function ccR_Mage_Fingers_of_Frost()
	if ccrPlayerHasBuff(FINGERS_OF_FROST) then
		ccR_Mage_Deep_Freeze()
		ccR_Mage_Ice_Lance()
		return
	end
	return
end

function ccR_Mage_Icy_Veins()
	if ccrPlayerHasSpell(ICY_VEINS)
	and ccrSpellIsReady(ICY_VEINS)
	and ccrPlayerIsInCombat()
	and ccrValidTarget()
	then
		ccrCast(ICY_VEINS)
		return
	end
	return
end

function ccR_Mage_Ice_Lance()
	if ccrPlayerHasSpell(ICE_LANCE)
	and ccrSpellCanAttack(ICE_LANCE)
	and ccrValidTarget()
	and Player:IsFacing(Target)
	then
		ccrCastAttack(ICE_LANCE)
		return
	end
	return
end

function ccR_Mage_Cone_of_Cold()
	if ccrPlayerHasSpell(CONE_OF_COLD)
	and ccrSpellIsReady(CONE_OF_COLD)
	and ccrValidTarget()
	and Player:IsFacing(Target)
	and Target:GetDistance(Player) <= 7
	then
		ccrCastAttack(CONE_OF_COLD)
		return
	end
	return
end

function ccR_Mage_Frost_Moving()
	if ccrPlayerIsMoving() then
		ccR_Mage_Nether_Tempest()
		ccR_Mage_Living_Bomb()

		if not ccrPlayerHasBuff(ICE_FLOES) then
			-- AoE While Moving
			if #tblPGetMeleeEnemies >= 3 then
				ccR_Mage_Arcane_Explosion()
			end
			-- Any Instant casts we can do
			if ccrValidTarget()
			and Player:IsFacing(Target)
			then
				ccR_Mage_Frost_Brain_Freeze()
				ccR_Mage_Cone_of_Cold()
				ccR_Mage_FireBlast()
				ccR_Mage_Ice_Lance()
			end
		end
	end
end

function ccR_Mage_Ice_Lance_or_Frostbolt()
	local vSwitchToIceLance = false
	if ((not ccrTargetIsElite() and ccrTargetHealth() <= 40) or (ccrTargetIsElite() and ccrTargetHealth() <= 15))
	and not ccrTargetIsBoss()
	then 
		vSwitchToIceLance = true
	end

	if vSwitchToIceLance then
		ccR_Mage_Ice_Lance()
	else
		ccR_Mage_Frostbolt()
	end
end

function ccR_Mage_Blizzard()
	if ccrPlayerHasSpell(BLIZZARD)
	and ccrSpellIsReady(BLIZZARD)
	and	not ccrPlayerIsCastingOrChanneling()
	and not ccrPlayerIsMoving()
	and Target and Target:Exists() and Target:GetDistance() <= 34
	then
		ccrCast(BLIZZARD)
		if SpellIsTargeting() == 1 then CastAtLocation(Target:GetLocation()) end
	end
end

function ccR_Mage_Frozen_Orb()
	if ccrPlayerHasSpell(FROZEN_ORB)
	and ccrSpellCanAttack(FROZEN_ORB)
	and Target and Target:Exists() and Player:IsFacing(Target)
	then
		ccrCast(FROZEN_ORB)
		return
	end
	return
end

function ccR_Mage_Frost_Brain_Freeze()
	if ccrPlayerHasBuff(BRAIN_FREEZE)
	and ccrSpellCanAttack(FROSTFIRE_BOLT)
	then
		ccrCastAttack(FROSTFIRE_BOLT)
		return
	end
	return
end
